package com.common.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.testng.annotations.BeforeClass;

	/**
		@author 王嘉犇
		@name 接口测试基类
	 */

public class InterfaceTestBase extends TestBase {
	
    @BeforeClass
    public void beforeclass() throws Exception{
    	log.info("===============BeforeClass===============");
    	try {
			InitParameterExcel ipe = new InitParameterExcel(this);
			ipe.setparamfileidcolumnnumric("case");
			ipe.cleanresultsheet("testresult");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    public Object[][] getDataBySheetName(String sheetName) throws IOException{
        // TODO Auto-generated method stub
    	Object[][] data;
    	InputStream is = getInputStream();
		HSSFWorkbook workbook = new HSSFWorkbook(is);
        Sheet sheet = workbook.getSheet(sheetName);
        int numberOfRows = sheet.getLastRowNum() + 1;
        int numberOfColumns = 2;
        data = new Object[numberOfRows - 1][numberOfColumns];
        for(int rowNum=1;rowNum<numberOfRows;rowNum++){
            Row row = sheet.getRow(rowNum);
            if(isEmpty(row)){
                break;
            }else{
                for(int column=0;column<numberOfColumns;column++){
                    Cell cell = row.getCell(column);
                    if(cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) {
                        data[rowNum - 1][column] = "";
                    }else {
                        data[rowNum - 1][column] = objectFrom(cell);
                    }
                }
            }
        }
        workbook.close();
        is.close();
        return data;
    }

    //得到当前测试类的同名excel文件的is流
    public InputStream getInputStream() throws FileNotFoundException{
    	File file = getDataDriverFile();
    	InputStream is = new FileInputStream(file);
    	return is;
    }
    
    private File getDataDriverFile(){
    	File file = new File("src/main/resources/datadriver/" + this.getClass().getSimpleName() + ".xls");
    	return file;
    }
    
    private File getTestResultFile(){
    	File file = new File("src/main/resources/testresult/" + this.getClass().getSimpleName() + "testresult.xls");
    	return file;
    }
    
    public String getDataDriverFilePath(){
    	File datadriverfile = getDataDriverFile();
    	String absolutepath = datadriverfile.getAbsolutePath();
    	return absolutepath;
    }
    
    public String getTestResultFilePath(){
    	File testresultfile = getTestResultFile();
    	String absolutepath = testresultfile.getAbsolutePath();
    	return absolutepath;
    }
    
    private boolean isEmpty(Row row) {
        // TODO Auto-generated method stub
        Cell firstCell = row.getCell(0);
        boolean rowIsEmpty = (firstCell == null)
                || (firstCell.getCellType() == Cell.CELL_TYPE_BLANK);
        return rowIsEmpty;
    }
    
    private Object objectFrom(Cell cell) {
        // TODO Auto-generated method stub
        Object cellValue = null;
        if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
            cellValue = cell.getRichStringCellValue().getString();
        } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            cellValue = cell.getNumericCellValue();
        } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
            cellValue = cell.getBooleanCellValue();
        } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
            cellValue = cell.getCellFormula();
        }
        return cellValue;
    }
}
